<?php
class ModelDesignBanner extends Model {
	public function addBanner($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "banner SET `name` = '" . $this->db->escape($data['name']) . 
			"', position_type = '" . (int)$data['position_type'] . 
			"', position_id = '" . (int)$data['position_id'] . 
			"', status = '" . (int)$data['status'] . "'");
		$banner_id = $this->db->getLastId();
	
		if (isset($data['banner_image'])) {
			foreach ($data['banner_image'] as $banner_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image SET banner_id = '" . (int)$banner_id . 
					"', link = '" .  $this->db->escape($banner_image['link']) . 
					"', image = '" .  $this->db->escape($banner_image['image']) . 
					"', sort_order = '" .  (int)$banner_image['sort_order'] . "'");
				$banner_image_id = $this->db->getLastId();
				
				foreach ($banner_image['banner_image_description'] as $language_id => $value) {				
					$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image_description SET banner_image_id = '" . (int)$banner_image_id . 
						"', language_id = '" . (int)$language_id . 
						"', banner_id = '" . (int)$banner_id . 
						"', title = '" .  $this->db->escape($value['title']) . "'");
				}
			}
		}		
	}
	
	public function editBanner($banner_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "banner SET `name` = '" . $this->db->escape($data['name']) . 
			"', position_type = '" . (int)$data['position_type'] . 
			"', position_id = '" . (int)$data['position_id'] . 
			"', status = '" . (int)$data['status'] . 
			"' WHERE banner_id = '" . (int)$banner_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "banner_image WHERE banner_id = '" . (int)$banner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner_image_description WHERE banner_id = '" . (int)$banner_id . "'");
		if (isset($data['banner_image'])) {
			foreach ($data['banner_image'] as $banner_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image SET banner_id = '" . (int)$banner_id . 
					"', link = '" .  $this->db->escape($banner_image['link']) . 
					"', image = '" .  $this->db->escape($banner_image['image']) . 
					"', sort_order = '" .  (int)$banner_image['sort_order'] . "'");
				$banner_image_id = $this->db->getLastId();
				
				foreach ($banner_image['banner_image_description'] as $language_id => $value) {				
					$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image_description SET banner_image_id = '" . (int)$banner_image_id . 
						"', language_id = '" . (int)$language_id . 
						"', banner_id = '" . (int)$banner_id . 
						"', title = '" .  $this->db->escape($value['title']) . "'");
				}
			}
		}			
	}
	
	public function getBanner($banner_id) {
		$lang_id = (int)$this->config->get('config_language_id');
		$query = $this->db->query("SELECT b.*, cd.display_name category_display_name, pd.display_name promotion_display_name FROM " . 
			DB_PREFIX . "banner b LEFT JOIN " . 
			DB_PREFIX . "category_description cd ON b.position_type = 1 AND b.position_id = cd.category_id AND cd.language_id = '" . $lang_id . "' LEFT JOIN  " . 
			DB_PREFIX . "promotion_description pd ON b.position_type = 2 AND b.position_id = pd.promotion_id AND pd.language_id = '" . $lang_id . 
			"' WHERE b.banner_id = '" . (int)$banner_id . "'");
		return $query->row;
	}
		
	public function getBannerImages($banner_id) {
		$banner_image_data = array();
		$banner_image_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner_image WHERE banner_id = '" . (int)$banner_id . "' ORDER BY sort_order");
		foreach ($banner_image_query->rows as $banner_image) {
			$banner_image_description_data = array();
			$banner_image_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner_image_description WHERE banner_image_id = '" . 
				(int)$banner_image['banner_image_id'] . "'");
			foreach ($banner_image_description_query->rows as $banner_image_description) {			
				$banner_image_description_data[$banner_image_description['language_id']] = array('title' => $banner_image_description['title']);
			}
		
			$banner_image_data[] = array(
				'link'                     => $banner_image['link'],
				'image'                    => $banner_image['image'],
				'sort_order'               => $banner_image['sort_order'],
				'banner_image_description' => $banner_image_description_data
			);
		}
		
		return $banner_image_data;
	}
		
	public function getBanners($data = array()) {
		$lang_id = (int)$this->config->get('config_language_id');
		$sql = "SELECT b.*, cd.display_name category_display_name, pd.display_name promotion_display_name FROM " . 
			DB_PREFIX . "banner b LEFT JOIN " . 
			DB_PREFIX . "category_description cd ON b.position_type = 1 AND b.position_id = cd.category_id AND cd.language_id = '" . $lang_id . "' LEFT JOIN  " . 
			DB_PREFIX . "promotion_description pd ON b.position_type = 2 AND b.position_id = pd.promotion_id AND pd.language_id = '" . $lang_id . "'";
		
		$sql_filter = "";
		if (isset($data['filter_name'])) {
			$sql_filter .= " AND LCASE(b.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		if (isset($data['filter_position_type'])) {
			$sql_filter .= " AND b.position_type = '" . $this->db->escape($data['filter_position_type']) . "'";
		}
		if (isset($data['filter_status'])) {
			$sql_filter .= " AND b.status = '" . $this->db->escape($data['filter_status']) . "'";
		}
		if (isset($data['filter_position_name'])) {
			$sql_filter .= " AND (cd.display_name LIKE '" . $this->db->escape(utf8_strtolower($data['filter_position_name'])) . 
				"%' OR pd.display_name LIKE '" . $this->db->escape(utf8_strtolower($data['filter_position_name'])) . "%')";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
		
		$sql .= " ORDER BY b.name";	
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		$query = $this->db->query($sql);
		return $query->rows;
	}
	public function getTotalBanners($data = array()) {
		$lang_id = (int)$this->config->get('config_language_id');
		$sql = "SELECT COUNT(*) AS total FROM " . 
			DB_PREFIX . "banner b LEFT JOIN " . 
			DB_PREFIX . "category_description cd ON b.position_type = 1 AND b.position_id = cd.category_id AND cd.language_id = '" . $lang_id . "' LEFT JOIN  " . 
			DB_PREFIX . "promotion_description pd ON b.position_type = 2 AND b.position_id = pd.promotion_id AND pd.language_id = '" . $lang_id . "'";
		
		$sql_filter = "";
		if (isset($data['filter_name'])) {
			$sql_filter .= " AND LCASE(b.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		if (isset($data['filter_position_type'])) {
			$sql_filter .= " AND b.position_type = '" . $this->db->escape($data['filter_position_type']) . "'";
		}
		if (isset($data['filter_status'])) {
			$sql_filter .= " AND b.status = '" . $this->db->escape($data['filter_status']) . "'";
		}
		if (isset($data['filter_position_name'])) {
			$sql_filter .= " AND (cd.display_name LIKE '" . $this->db->escape(utf8_strtolower($data['filter_position_name'])) . 
				"%' OR pd.display_name LIKE '" . $this->db->escape(utf8_strtolower($data['filter_position_name'])) . "%')";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
		
      	$query = $this->db->query($sql);
		return $query->row['total'];
	}	
}
?>